package com.huabo.system.oracle.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huabo.system.oracle.entity.TblAccBook;
import com.huabo.system.oracle.entity.TblAcctBook;
import com.huabo.system.oracle.entity.TblSystemModelFlow;
import com.huabo.system.oracle.entity.TblSystemModule;
import com.hbfk.util.PageInfo;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

public interface TblSystemModuleMapper extends BaseMapper<TblSystemModule> {


    @SelectProvider(method="selectPageInfoByOrgId",type=TblSystemModuleMapperSqlConifg.class)
    List<TblSystemModule> selectPageInfoByOrgId(PageInfo<TblSystemModule> pageInfo, BigDecimal orgid,TblSystemModule module);



    @SelectProvider(method="selectCountByPageInfo",type=TblSystemModuleMapperSqlConifg.class)
    Integer selectCountByPageInfo(BigDecimal orgid, TblSystemModule module);


    @Select("SELECT COUNT(*) FROM TBL_SYSTEM_MODULE M LEFT JOIN TBL_ORGANIZATION O ON M.MODELORG = O.ORGID WHERE M.MODELID != #{modelId} AND M.MODELNAME = #{modelName} AND O.ORGID = #{orgid}")
    Integer selectUpdateCount(BigDecimal orgid, Integer modelId, String modelName);

    @Select("SELECT COUNT(*) FROM TBL_SYSTEM_MODULE M LEFT JOIN TBL_ORGANIZATION O ON M.MODELORG = O.ORGID WHERE (M.MODELNO = #{modelNo} OR M.MODELNAME = #{modelName}) AND O.ORGID = #{orgid}")
    Integer selectInsertCount(BigDecimal orgid, String modelNo, String modelName);

    @Update("UPDATE TBL_SYSTEM_MODULE SET MODELSTATUS = #{moduleStatus} WHERE MODELID = #{moduleId}")
    void updateByModuleId(Integer moduleStatus, Integer moduleId);

    @Select("SELECT MODELTYPE FROM TBL_SYSTEM_MODULE WHERE MODELID = #{moduleId}")
    String selectByModuleId(Integer moduleId);

    @Select("SELECT COUNT(0) FROM TBL_ORG_RIGHT WHERE ORGID = #{id} AND RIGHTID = #{rightId}")
    Integer selectOrgManageRightId(String id, String rightId);

    @Insert("INSERT INTO TBL_ORG_RIGHT (ORGID,RIGHTID) VALUES (#{id},#{rightId})")
    void insertModule(String id, String rightId);

    @Insert("INSERT INTO TBL_ORG_RIGHT (ORGID,RIGHTID) SELECT #{id} AS ORGID,RIGHTID FROM TBL_MANAGE_RIGHT START WITH RIGHTID = #{rightId} CONNECT BY PRIOR RIGHTID = FATHERRIGHTID")
    void insertModuleByrightId(String id, String rightId);

    @Delete("DELETE FROM TBL_SYSTEM_MODELORG WHERE ORGID = #{orgId} AND MODELID = #{moduleId}")
    void deleteByIdAndModuleId(Integer moduleId, String orgId);

    @Insert("INSERT INTO TBL_SYSTEM_MODELORG(MODELID,ORGID) VALUES(#{moduleId},#{orgId})")
    void insertByIdAndModuleId(Integer moduleId, String orgId);

//    @Delete("DELETE FROM TBL_SYSTEM_MODELORG WHERE MODELID = #{moduleId} AND ORGID IN (#{orgId})")
//    void deleteBymoduleIdAndOrgId(Integer moduleId, String orgId);

    @Delete("\tDELETE FROM TBL_SYSTEM_MODELORG WHERE MODELID = #{moduleId} AND ORGID IN (SELECT\n" +
            "\tREGEXP_SUBSTR( (#{orgId}), '[^,]+', 1, LEVEL ) \n" +
            "FROM\n" +
            "\tDUAL CONNECT BY REGEXP_SUBSTR( (#{orgId}), '[^,]+', 1, LEVEL ) IS NOT NULL)")
    void deleteBymoduleIdAndOrgId(Integer moduleId, String orgId);

    @Select("SELECT * FROM TBL_SYSTEM_MODULE WHERE MODELID = #{moduleId}")
    TblSystemModule selectBymoduleId(Integer moduleId);

    @InsertProvider(method="insertSystemModule",type=TblSystemModuleMapperSqlConifg.class)
    void insertSystemModule(TblSystemModule module);

    @Delete("DELETE FROM TBL_SYSTEM_MODELFLOW WHERE MODELID = #{modelId}")
    void deleteByModulId(Integer modelId);

    @UpdateProvider(method="updateByModule",type=TblSystemModuleMapperSqlConifg.class)
    void updateByModule(TblSystemModule oldModule);

    @Delete("DELETE FROM TBL_SYSTEM_MODELFLOW WHERE MODELID = #{modelId} and FLOWID= #{flowid}")
    void removeModelFlowRelation(@Param("flowid") String flowid, @Param("modelId")Integer modelId);

    @Select("SELECT * FROM TBL_SYSTEM_MODELFLOW WHERE MODELID = #{modelId}")
    List<TblSystemModelFlow> findFlowByModule(Integer modelId);


}
